const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    declarename: '',
    declareponit: '',
    declaredesc: '',
    materialpath: [],
    isdeclare: 0, // 0是我的申报详情  1是申报
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(opts) {
    console.log(opts, opts.isdeclare);
    this.setData(opts, () => {
      if (opts.isdeclare === '1') { // 申报
        this.loadDetail();
      } else { // 我的申报详情
        this.loadDeclareDetail();
      }
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  // 我的申报详情
  loadDeclareDetail() {
    $util.request({
      url: 'entry/wxapp/partywork.appraisal.viewdeclare',
      method: 'GET',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        this.setData(res.data);
        res.data.sname && wx.setNavigationBarTitle({
          title: res.data.sname,
        });
      },
    });
  },
  // 申报
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/partywork.appraisal.raisaldeclare',
      method: 'GET',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        this.setData(res.data);
        wx.setNavigationBarTitle({
          title: '申报',
        });
      },
    });
  },
  imgsOnChange(e) {
    this.setData({
      materialpath: e.detail,
    });
  },
  bindInputValue(e) {
    let {
      id,
    } = e.currentTarget.dataset;

    let { nscore, spointlimit } = this.data;
    if (id === 'declareponit') {
      var re = /^[0-9]+.?[0-9]*/;
      if (!re.test(e.detail.value) || (e.detail.value > spointlimit - nscore)) {
        this.setData({
          [id]: '',
        });
        return wx.showToast({
          title: `您还可以申请${spointlimit - nscore}分`,
          icon: 'none',
          duration: 1500,
        })
  　　} 
    }

    this.setData({
      [id]: e.detail.value,
    });
  },
  submit() {
    let {
      id,
      declarename,
      declareponit,
      declaredesc,
      nscore,
      spoint,
      spointlimit,
      materialpath,
    } = this.data;
    if (!declarename.trim()) {
      wx.showModal({
        title: '提示',
        content: '请填写申报主题',
        showCancel: false,
      });
      return;
    }
    if (!declareponit.trim() || Number(declareponit) > Number(spoint) || (Number(declareponit) + Number(nscore)) > Number(spointlimit)) {
      wx.showModal({
        title: '提示',
        content: `请填写正确的申报分值，单次申报分值不大于${spoint},总申报分值不大于${spointlimit}`,
        showCancel: false,
      });
      return;
    }
    if (!declaredesc.trim()) {
      wx.showModal({
        title: '提示',
        content: `请输入申报说明`,
        showCancel: false,
      });
      return;
    }
    if (this.isSubmit) return;
    this.isSubmit = true;
    $util.request({
      url: 'entry/wxapp/partywork.appraisal.raisaldeclare',
      method: 'POST',
      data: {
        id,
        declarename,
        declareponit,
        declaredesc,
        materialpath,
      },
      success: (res) => {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          mask: true,
          duration: 1500,
          success: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 500);
          },
        });
      },
      complete: () => {
        this.isSubmit = false;
      },
    });
  },
});